//
//  ICMAddBelongsView.m
//  ICrEAM
//
//  Created by rainimute on 16/10/27.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMAddBelongsView.h"

@interface ICMAddBelongsView()

@property (nonatomic, strong) NSArray *belongTitles;

@property (nonatomic, strong) NSMutableArray *belongButtons;

@end

@implementation ICMAddBelongsView
#pragma mark    初始化
- (instancetype)initAddBelongsViewWithBelongs:(NSArray*)belongs{
    if (self = [super initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)]) {
        
        self.belongs = belongs?[belongs mutableCopy]:[NSMutableArray array];
        
        UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        backgroundView.backgroundColor = [UIColor clearColor];
        UITapGestureRecognizer *backTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backgroundViewTapAction:)];
        [backgroundView addGestureRecognizer:backTap];
        [self addSubview:backgroundView];
        //
        UIView *selectBelongsBackView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight-420, 180, 360)];
        [selectBelongsBackView centerXInSuperView:self ratio:0.45];
        selectBelongsBackView.backgroundColor = [UIColor whiteColor];
        for (UIButton *belongButton in self.belongButtons) {
            [selectBelongsBackView addSubview:belongButton];
            [belongButton centerXInSuperView:selectBelongsBackView];
        }
        //
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 180, 20)];
        [titleLabel setUpLabelWithText:@"随车物品" alignment:NSTextAlignmentCenter textSize:14 textColor:TitleColor];
        [selectBelongsBackView addSubview:titleLabel];
        //
        UILabel *sepLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 52.5, 120, 1)];
        sepLabel.backgroundColor = [UIColor grayColor];
        [selectBelongsBackView addSubview:sepLabel];
        [self addSubview:selectBelongsBackView];
        
    }
    return self;
}

+ (instancetype)creatAddBelongsViewWithBelongs:(NSArray*)belongs{
    return [[ICMAddBelongsView alloc]initAddBelongsViewWithBelongs:belongs];
}

/*action*/
- (void)backgroundViewTapAction:(UITapGestureRecognizer*)tap{
    [self removeFromSuperview];
}

- (void)belongButtonAction:(UIButton*)sender{
    sender.selected = !sender.selected;
    sender.backgroundColor = sender.selected?TitleColor:[UIColor whiteColor];
    NSString *title = sender.titleLabel.text;
    if (sender.selected) {
        if (![self.belongs containsObject:title]) {
            [self.belongs PACA_AddObject:title];
        }
    }else{
        if ([self.belongs containsObject:title]) {
            [self.belongs removeObject:title];
        }
    }
    if ([self.delegate respondsToSelector:@selector(addBelongsViewDidSelectedBelongs:)]) {
        [self.delegate addBelongsViewDidSelectedBelongs:self.belongs];
    }
}

/*init*/
- (NSArray *)belongTitles{
    if (!_belongTitles) {
        _belongTitles = [ICrEAMTool isVW]?@[@"太阳镜",@"随车工具及备胎",@"酒",@"运动器材",@"独立导航",@"香烟"]:@[@"备胎",@"随车工具"];
    }
    return _belongTitles;
}

- (NSMutableArray *)belongButtons{
    if (!_belongButtons) {
        _belongButtons = [NSMutableArray array];
        for (NSInteger i=0; i<self.belongTitles.count; i++) {
            UIButton *belongButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 75+i*45, 65, 35)];
            [belongButton setUpButtonWithNormolTitle:self.belongTitles[i] selectedTitle:self.belongTitles[i] normolTitleSize:14 selectedTitleSize:14 normolTitleColor:[UIColor grayColor] selectedTitleColor:[UIColor whiteColor] normolBcakImage:nil selectedBackImage:nil actionSEL:@selector(belongButtonAction:) target:self];
            BorderWidth(belongButton, 1);
            belongButton.layer.borderColor = [UIColor grayColor].CGColor;
            belongButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
            belongButton.titleLabel.numberOfLines = 0;
            belongButton.selected = [self.belongs containsObject:self.belongTitles[i]];
            belongButton.backgroundColor = belongButton.selected?TitleColor:[UIColor whiteColor];
            [_belongButtons PACA_AddObject:belongButton];
        }
    }
    return _belongButtons;
}

- (NSMutableArray *)belongs{
    if (!_belongs) {
        _belongs = [NSMutableArray array];
    }
    return _belongs;
}


@end
